<?php session_start();
if(!isset($_SESSION['login']))
{
    $_SESSION['login'] = false;
    $_SESSION['username'] = "dummy";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Register</title>

    <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
	<link rel="stylesheet" type="text/css" title="T1" href="stylesheet/posting.css" />
		<meta name="description" content="RERGag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta name="author" content="Robertus Theodore" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
        <div class="wrapper">
        <div class="header">
            <a href="index.php" id="image"><img src="images/logo.png" alt="title" height="50"/></a>
            <h1>RERGAG - Just For Laugh</h1>
        </div>

        <div class="links">
            <ul>
                <li><a href="registrasi.php" id="registrasi_link" >Registrasi</a></li>
                <li><a href="index.php" id="home_link">Home</a></li>
                <li><a href="daftar.php" id="daftar_link">Daftar</a></li>
                <li><a href="posting.php" id="upload_link" class="active">Posting</a></li>
            </ul>
        </div>
        </div>
		
        <div class="main_container">
            <div id="preview"></div>
	
	<div align="left" style="width: 500px;" >

	<form id="posting_form" name="posting_form" method="post" action="profile.php" onsubmit="checkOnSubmit()" enctype="multipart/form-data">
            <fieldset><legend>Posting Baru</legend>
                    
                    <label class="label_posting">e-mail</label><input class="input_posting" id="email" type="text" name="email"></input><br />
                    
                    <label class="label_posting">About Me</label><textarea class="textarea_posting" id="about_me" name="about_me" rows="5" cols="25"></textarea><br />
                    
                    <label class="label_posting">My Avatar </label><input class="input_posting" name="avatar" type="file" /><br />

                    <label class="label_posting">&nbsp;</label><input type="Submit" class="input_posting" value="Posting"/>
                    <input type="button" name="buttonPreview" class="input_posting" onclick="preview()" value="Preview"/>

            </fieldset>
        </form>
        <?php
            $con = mysql_connect("localhost","root","");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }

            mysql_select_db("progin_171_13509008", $con);

            // get next id_content for image naming
//            global $curIDContent;
//            $qCount = mysql_query("SELECT MAX(id_content) FROM content");
//            $results = mysql_fetch_array($qCount);
//            $curIDContent = $results['MAX(id_content)'] + 1;

            //get form value
            $email = $_POST['email'];
            if(isset($_POST['about_me'])){
                $about = $_POST['about_me'];
            }
            //cek email
            $checkEmail = true;
            $queryCheckEmail = "SELECT * FROM user where email='".$email."'";
            $resultCheckEmail = mysql_query($queryCheckEmail);
            $rowCheckEmail = mysql_num_rows($resultCheckEmail);
            if($rowCheckEmail == 0){
                $checkEmail = true;
            }else{
                $checkEmail = false;
            }
            
            if($checkEmail){
                if(isset($_FILES["avatar"]["type"])){
                //check image file type
                    if ($_FILES["avatar"]["type"] != "image/jpeg"){
                        echo "File belum terisi, tipe File seharusnya jpg/jpeg";
                    }
                    else {                        
                        if(is_uploaded_file($_FILES['avatar']['tmp_name'])){
                            $fileNameImage = $username.".jpg"; //get the file name
                            $move = move_uploaded_file($_FILES['avatar']['tmp_name'], 'C://xampp/htdocs/rergag/images/avatar/'.$fileNameImage); //save image to the folder
                            if ($move){
                                
                            }
                        }
                    }
                }
                if(isset($fileNameImage) && isset($about)){
                    $queryUpdateUser = "UPDATE user SET email='".$email."', about_me = '".$about."', avatar = 'images/avatar/".$fileNameImage."' where user_name ='".$_SESSION['username']."'";
                }
                else{
                    if(!isset($fileNameImage) && isset($about)){
                        $queryUpdateUser = "UPDATE user SET email='".$email."', about_me = '".$about."' where user_name ='".$_SESSION['username']."'";
                    }else{
                        if(isset($fileNameImage) && !isset($about)){
                            $queryUpdateUser = "UPDATE user SET email='".$email."', avatar = 'images/avatar/".$fileNameImage."' where user_name ='".$_SESSION['username']."'";
                        }else{

                        }
                    }
                }
                echo $queryUpdateUser;
                mysql_query($queryUpdateUser) or die ("GAGAL MELAKUKAN QUERY. " . mysql_error());
                echo "Registration Success";
            }else{
                //$_SESSION['id_content']=$curIDContent;
                if(!isset($email)){
                    
                }else{
                    echo "<script>document.location.href='register.php'</script>";
                    if(!$checkEmail){
                        echo "Email was already used";
                    }
                }
            }

        ?>
        </div>
        <script type="text/javascript" src="script/posting.js"></script>
        <script type="text/javascript" src="script/progin.js"></script>
        </div>
    </body>
</html>
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
